package com.araaya.eslimsmobile.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseFunction extends SQLiteOpenHelper {

	// All Static variables
	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "ElandMobile";

	// Contacts table name
	private static final String TABLE_GPS_LAND = "GpsLand";
	
	// Contacts Table Columns names
	private static final String KEY_ID = "id";
	private static final String KEY_LAND_NAME = "landname";
	private static final String KEY_LONGITUDE = "longitude";
	private static final String KEY_LATITUDE = "latitude";
	private static final String KEY_ALTITUDE = "altitude";
	private static final String KEY_CORDINATES = "coordinates";

	public DatabaseFunction(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_GPS_LAND + "("
				+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_LAND_NAME + " TEXT,"
				+ KEY_LONGITUDE + " TEXT," + KEY_LATITUDE + " TEXT,"
				+ KEY_ALTITUDE + " TEXT," + KEY_CORDINATES + " TEXT)";
		
		db.execSQL(CREATE_CONTACTS_TABLE);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_GPS_LAND);
		// Create tables again
		onCreate(db);
	}

	// Adding new Land
	public long addGPSLand(GPSLand gpsLand) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_LAND_NAME, gpsLand.getLandName());
		values.put(KEY_LONGITUDE, gpsLand.getLongitude());
		values.put(KEY_LATITUDE, gpsLand.getLatitude());
		values.put(KEY_ALTITUDE, gpsLand.getAltitude());
		values.put(KEY_CORDINATES, gpsLand.getCoordinates());

		Log.d("SAVE==>", gpsLand.getLandName());

		// Inserting Row
		long res = db.insert(TABLE_GPS_LAND, null, values);
		db.close(); // Closing database connection
		return res;
	}

	// Getting single Land
	public GPSLand getGPSLand(int id) {
		SQLiteDatabase db = this.getReadableDatabase();

		Cursor cursor = db.query(TABLE_GPS_LAND, new String[] { KEY_ID,
				KEY_LAND_NAME, KEY_LONGITUDE, KEY_LATITUDE, KEY_ALTITUDE,
				KEY_CORDINATES }, KEY_ID + "=?",
				new String[] { String.valueOf(id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();

		GPSLand gpsLand = new GPSLand(Integer.parseInt(cursor.getString(0)),
				cursor.getString(1), cursor.getString(2), cursor.getString(3),
				cursor.getString(4), cursor.getString(5));

		cursor.close();
		db.close();
		return gpsLand;
	}

	// Getting All Land
	public List<GPSLand> getAllGPSLands() {
		List<GPSLand> gpsLands = new ArrayList<GPSLand>();
		// Select All Query
		String selectQuery = "SELECT  * FROM " + TABLE_GPS_LAND;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(selectQuery, null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				GPSLand land = new GPSLand();
				land.setId(Integer.parseInt(cursor.getString(0)));
				land.setLandName(cursor.getString(1));
				land.setLongitude(cursor.getString(2));
				land.setLatitude(cursor.getString(3));
				land.setAltitude(cursor.getString(4));
				land.setCoordinates(cursor.getString(5));

				// Adding land to list
				gpsLands.add(land);
			} while (cursor.moveToNext());
		}
		cursor.close();
		db.close();

		// return land list
		return gpsLands;
	}

	// Getting All Land
	public List<GPSLand> getGPSLandsByName(String landName) {
		List<GPSLand> gpsLands = new ArrayList<GPSLand>();
		// Select All Query
		// String selectQuery = "SELECT  * FROM " + TABLE_GPS_LAND;

		SQLiteDatabase db = this.getReadableDatabase();
		// Cursor cursor = db.rawQuery(selectQuery, null);

		Cursor cursor = db
				.rawQuery("select * FROM " + TABLE_GPS_LAND + " WHERE "
						+ KEY_LAND_NAME + " LIKE '" + landName + "%'", null);

		// looping through all rows and adding to list
		if (cursor.moveToFirst()) {
			do {
				GPSLand land = new GPSLand();
				land.setId(Integer.parseInt(cursor.getString(0)));
				land.setLandName(cursor.getString(1));
				land.setLongitude(cursor.getString(2));
				land.setLatitude(cursor.getString(3));
				land.setAltitude(cursor.getString(4));
				land.setCoordinates(cursor.getString(5));

				// Adding land to list
				gpsLands.add(land);
			} while (cursor.moveToNext());
		}
		cursor.close();
		db.close();

		// return land list
		return gpsLands;
	}

	// Updating single contact
	public int updateGPSLand(GPSLand gpsLand) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(KEY_LAND_NAME, gpsLand.getLandName());
		values.put(KEY_LONGITUDE, gpsLand.getLongitude());
		values.put(KEY_LATITUDE, gpsLand.getLatitude());
		values.put(KEY_ALTITUDE, gpsLand.getAltitude());
		values.put(KEY_CORDINATES, gpsLand.getCoordinates());

		// updating row
		return db.update(TABLE_GPS_LAND, values, KEY_ID + " = ?",
				new String[] { String.valueOf(gpsLand.getId()) });
	}

	// Deleting single land
	public void deleteGPSLand(GPSLand gpsLand) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_GPS_LAND, KEY_ID + " = ?",
				new String[] { String.valueOf(gpsLand.getId()) });
		db.close();
	}

	// Getting land Count
	public int getGPSLandsCount() {
		String countQuery = "SELECT  * FROM " + TABLE_GPS_LAND;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(countQuery, null);
		cursor.close();

		// return count
		return cursor.getCount();
	}

}
